Skip to content

Add DynamoDB state adapter#281

Open
anatolzak wants to merge 4 commits intovercel:mainfrom
anatolzak:feat/dynamodb-state-adapter
Open

Add DynamoDB state adapter#281
anatolzak wants to merge 4 commits intovercel:mainfrom
anatolzak:feat/dynamodb-state-adapter

Conversation

@anatolzak
Copy link

@anatolzak anatolzak commented Mar 21, 2026

Closes #282

Summary

  • Adds @chat-adapter/state-dynamodb — a serverless-native state adapter using DynamoDB single-table design
  • Configurable pkName, skName, and ttlName options for mapping to existing table schemas

Design

  • Single table (configurable key attribute names, defaults: pk/sk) with all record types (subscriptions, locks, cache, lists)
  • Atomic lock operations via DynamoDB conditional expressions
  • Dual-layer TTL: DynamoDB native TTL (configurable attribute, default expiresAt) for GC + app-level expiresAtMs checks for correctness
  • List ordering via atomic counter (ADD seq :one) + 16-digit zero-padded sort keys
  • Values stored as native DynamoDB types (no JSON serialization)
  • Best-effort list trimming with unprocessed item logging

Test plan

  • pnpm validate passes (typecheck, lint, knip, tests, build)
  • 53 unit tests with mocked DynamoDBDocument client
  • Tests for custom pkName, skName, ttlName across all code paths (subscribe, get, lock, cache, list trim, getList, refreshListTtl)
  • Manual test with DynamoDB Local + example Next.js app

🤖 Generated with Claude Code

anatolzak and others added 2 commits March 21, 2026 21:19
Adds @chat-adapter/state-dynamodb, a serverless-native state adapter
using DynamoDB single-table design with DynamoDBDocument client.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Users typically already have @aws-sdk in their project. Peer deps
avoid version conflicts and reduce bundle duplication.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@vercel
Copy link
Contributor

vercel bot commented Mar 21, 2026

@anatolzak is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

@socket-security
Copy link

socket-security bot commented Mar 21, 2026

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Added@​aws-sdk/​client-dynamodb@​3.1014.09810010098100
Added@​aws-sdk/​lib-dynamodb@​3.1014.010010010098100

View full report

@cramforce
Copy link
Collaborator

I'm not so sure about this one. Should it live inside or outside the repo? Would people actually use this?

@padapada09
Copy link

I would 100% use this, dynamodb is perfect for 100% serverless applications running on aws. Otherwise I'm forced to use a persistant state alternative.

Extracts DynamoDBStateSharedOptions to avoid duplicating options
across interfaces. Adds pkName, skName, and ttlName options so users
can map to existing table schemas. Includes tests for all custom
attribute name paths (subscribe, get, lock, cache, list trim, getList,
refreshListTtl).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
ProjectionExpression in isSubscribed and trimList used raw attribute
names which would fail if pkName or skName is a DynamoDB reserved word.
Now uses #pk and #sk placeholders everywhere.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@padapada09
Copy link

If it's worth something, I copied your implementation @anatolzak and it's working great. Only thing to consider is, optionally let me define the pk and sk attribute names to use, so that I can use it on my existing dynamodb table that might have a different pk and sk naming (in my case I use PK and SK in upper case). But might be better to have that fixed, not sure, just wanted to suggest that.

@anatolzak
Copy link
Author

@padapada09 thanks for the suggestion! I actually already did that right after your first comment 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add DynamoDB state adapter

3 participants